 <template>
  <div class="content">
    <THeader :active="this.$route.path"></THeader>
    <el-row style="padding: 40px 0;">
      <el-col :span="18" :offset="3" class="body-card">
        <h3>
          <span @click="routerBack" style="cursor: pointer">
            <i class="el-icon-arrow-left"></i>记录旅游足迹</span> 
            <el-button  type="primary" @click="sharefootprint" :disabled="disabled">分享足迹(你的好友可见)</el-button>
        </h3>
        <Divider/>
        <el-form ref="form" :model="recordForm" label-width="80px">
          <el-form-item label="标题">
            <span v-if="isShow" v-text="recordForm.title"></span>
            <el-input v-else placeholder="输入标题" v-model="recordForm.title"></el-input>
          </el-form-item>

          <el-form-item label="目录" v-if="!isShow">
            <el-select placeholder="选择目录" v-model="recordForm.srcId">
              <el-option v-for="(item,index) in footprintSrc_items" :key="index" :value="item.id" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="简介">
            <span v-if="isShow" v-text="recordForm.brief"></span>
            <el-input v-else placeholder="输入简介（可为空）" v-model="recordForm.brief"></el-input>
          </el-form-item>
          <el-form-item label="游记内容">
            <span v-if="isShow" v-text="recordForm.text"></span>
            <el-input v-else placeholder="输入内容" v-model="recordForm.text" type="textarea" :rows="12"></el-input>
          </el-form-item>
          <el-form-item v-if="!isShow" style="text-align: right">
            <el-button style="width: 120px" @click="routerBack">取消</el-button>
            <el-button @click="recordAction" style="width: 120px">提交</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { footprintServer, footprintSrcListServer } from "../../api/footprints";
import { footprintshareServer } from "../../api/friend";
//记录足迹
export default {
  name: "recordFootprint",
  data() {
    return {
      modelVisible: false,
      recordForm: {
        id:"",	     
        title: "",
        brief: "",
        text: "",
        srcId: ""
      },
	  disabled:true,
      artId:"",
      footprintSrc_items: []
    };
  },
  mounted() {
    if (this.isShow) {
      this.getFootprintsData();
	  this.disabled=false;
    } else {
      this.getSrcList();
    }
    const { srcId } = this.$route.query;
    if (srcId) {
      this.recordForm.srcId = srcId;
    }
  },
  computed: {
    isShow() {
      return this.$route.name === "showFootprints";
    },
    srcId() {
      return this.$route.params.srcId;
    },
    fid() {
      return this.$route.params.fid;
    }
  },
  methods: {
    getSrcList() {
      footprintSrcListServer("get").then(
        suc => {
          const { code, data, message } = suc.data;
          if (code) {
            if (Array.isArray(data)) {
              this.footprintSrc_items = data;
            }
          } else {
            this.$message({
              type: "error",
              message
            });
          }
        },
        err => {
          this.$message({
            type: "error",
            message: err.message
          });
        }
      );
    },
    getFootprintsData() {
      footprintServer("get", {
        params: {
          fid: this.fid
        }
      }).then(
        suc => {
          const { code, message, data } = suc.data;
          if (code) {
            const {id, title, brief, footprint } = data;
            this.recordForm = {
			  id,
              title,
              brief,
              text: footprint
            };
          } else {
            this.$message({
              type: "error",
              message
            });
          }
        },
        err => {
          this.$message({
            type: "error",
            message: err.message
          });
        }
      );
    },
    sharefootprint(){
	 footprintshareServer("post",{
        params: {
		id:this.recordForm.id ,
        title:this.recordForm.title ,
        brief:this.recordForm.brief ,
        text: this.recordForm.text , 
        }
      }).then(
        suc => {
          const { code, message } = suc.data;
              if (code) {
                this.$message({
                  type: "success",
                  message
          }); 
          } else {
            this.$message({
              type: "error",
              message
            });
          }
        },
        err => {
          this.$message({
            type: "error",
            message: err.message
          });
        }
      );
	 
	},
    routerBack() {
      this.$router.push({
        path: `/travel-footprints`,
        query: {
          srcId: this.$route.query.srcId
        }
      });
    },
    recordAction() {
      footprintServer("post", {
        srcId: this.srcId,
        ...this.recordForm
      }).then(
        suc => {
          const { code, message } = suc.data;
          if (code) {
            this.$message({
              type: "success",
              message
            });
            this.routerBack();
          } else {
            this.$message({
              type: "error",
              message
            });
          }
        },
        err => {
          this.$message({
            type: "error",
            message: err.message
          });
        }
      );
    }
  }
};
</script>

<style lang="less">

</style>
